package bank;

import java.util.ArrayList;
import java.util.List;

public class _46Permute {
    public static List<List<Integer>> permute(int[] nums) {
        //  尝试回溯法
        List<List<Integer>> res = new ArrayList<>();
        backtrack(res,nums,new ArrayList<Integer>(),0);
        return res;
    }

    private static void backtrack(List<List<Integer>> res, int[] nums, ArrayList<Integer> list, int index) {
        if (index == nums.length){
            res.add(new ArrayList<>(list));
            return;
        }
        //  循环遍历当前集合
        for (int i = 0;i < nums.length;i++){
            //  如果list中包涵当前元素，则跳过
            if (!list.contains(nums[i])){
                list.add(nums[i]);
                backtrack(res,nums,list,index+1);
                list.remove(list.size()-1);
            }
        }
    }
}
